package szf.im.client.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import szf.im.client.data.MessageOuterClass;
import szf.im.client.util.Cfg;
import szf.im.client.util.SpringUtil;

/**
 * @author shizhanfu
 */
public class ProtoBufHandler extends SimpleChannelInboundHandler<MessageOuterClass.Message> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, MessageOuterClass.Message message) throws Exception {
        MessageOuterClass.Message.MsgType type = message.getMsgType();
        switch (type) {
            case sendTxt:
                System.out.println("用户 " + message.getUserId() + " say::" + message.getInfo());
                break;
            default:
                break;
        }
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Cfg cfg = SpringUtil.GetBean(Cfg.class);
        MessageOuterClass.Message msg = MessageOuterClass.Message.newBuilder()
                .setUserId(cfg.getuId()).setMsgType(MessageOuterClass.Message.MsgType.login).build();
        ctx.writeAndFlush(msg);
    }
}
